﻿@page "/Single_Table_Generate"
@using System.Reflection
@using BBCore.Core.Services
@using BBCore.Server.PublicComponentBase
@inject ToastService ToastService
<h3>单表Crud导入导出代码生成器(文件生成指定在C盘YourOutputPath文件夹下,以HYJ_Single_Table_CRUD为例子)</h3>
<Select TValue="string"
        Items="_entityItems" @bind-Value="_selectedValue"
        Placeholder="请选择实体类">
</Select>
<Button Color="Color.Primary"
        OnClick="@OnGetSelectedValue">
    生成代码
</Button>
@if (!string.IsNullOrEmpty(_selectedValue))
{
    <div class="mt-2">当前选中：@_selectedValue</div>
}
@code {
    private string? SelectedEntityType { get; set; }
    private string _Namespace = "BBCore.Core.Models.Samples";
    private string OutputPath = @"C:\YourOutputPath";
    private Type BaseTypeToExclude = typeof(BaseModel); // 替换为实际的基类类型
    private string? _selectedValue; // 存储下拉框的选中值（FullName）
    private List<SelectedItem> _entityItems = new();
    protected override void OnInitialized()
    {
        var entityTypes = GetEntityTypes();
        _entityItems = entityTypes
       .Select(t => new SelectedItem(t.FullName!, t.Name))
       .ToList();
        base.OnInitialized();
    }
    private IEnumerable<Type> GetEntityTypes()
    {
        GetModelClassName tmp = new GetModelClassName();
        return tmp.GetEntityTypes(_Namespace);
    }
    private async Task OnGetSelectedValue()
    {
        if (string.IsNullOrEmpty(_selectedValue))
        {
            return;
        }
        Type selectedType = GetEntityTypes().FirstOrDefault(t => t.FullName == _selectedValue)!;
        if (selectedType != null)
        {
            var generator = new Single_Table_CodeGenerator(selectedType, _Namespace, OutputPath, BaseTypeToExclude);
            generator.Single_Table_Generate();
            await ToastService.Show(new ToastOption()
                {
                    Title = "单表Crud导入导出",
                    Content = "代码已生成,请到YourOutputPath去查找,4秒后自动关闭",
                    Category = ToastCategory.Success
                });
        }
    }

}
